package com.sfh.tourist.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.provider.Settings;

import com.sfh.lib.ui.DialogBuilder;
import com.sfh.lib.ui.MVVMActivity;
import com.sfh.lib.ui.MVVMFragment;
import com.yanzhenjie.permission.AndPermission;

public class PermissionUtil {
    public interface PermissionCallBack {
        void onHasPermission();

        void onFailedPermission();
    }

    public static  void checkPermissions(MVVMActivity activity, String toast, int requestCode, PermissionCallBack callBack, String... permissions) {

        AndPermission.with(activity).
                runtime()
                .permission(permissions)
                .rationale((context, data, executor) -> {
                    DialogBuilder dialogBuilder = new DialogBuilder();
                    dialogBuilder.setMessage(toast);
                    dialogBuilder.setCancelTextListener("取消", (dialog, which) -> {
                        dialog.dismiss();
                        callBack.onFailedPermission();
                    });
                    dialogBuilder.setOKTextListener("去授权", (dialog, which) -> {
                        dialog.dismiss();
                        executor.execute();
                    });
                    activity.showDialog(dialogBuilder);
                }).onGranted(data -> callBack.onHasPermission()).onDenied(data -> {
            if (AndPermission.hasAlwaysDeniedPermission(activity,permissions)){
                callBack.onHasPermission();
                return;
            }

            if (AndPermission.hasAlwaysDeniedPermission(activity,permissions)){
                DialogBuilder dialogBuilder = new DialogBuilder();
                dialogBuilder.setMessage(toast);
                dialogBuilder.setCancelTextListener("取消", (dialog, which) -> {
                    dialog.dismiss();
                    callBack.onFailedPermission();
                });
                dialogBuilder.setOKTextListener("去授权", (dialog, which) -> {
                    dialog.dismiss();
                    AndPermission.with(activity).runtime().setting().start(requestCode);
                });
                activity.showDialog(dialogBuilder);
            }else{
                callBack.onFailedPermission();
            }
        }).start();
    }

    public static  void checkPermissions(MVVMFragment activity, String toast, int requestCode, PermissionCallBack callBack, String... permissions) {

        AndPermission.with(activity).
                runtime()
                .permission(permissions)
                .rationale((context, data, executor) -> {
                    DialogBuilder dialogBuilder = new DialogBuilder();
                    dialogBuilder.setMessage(toast);
                    dialogBuilder.setCancelTextListener("取消", (dialog, which) -> {
                        dialog.dismiss();
                        callBack.onFailedPermission();
                    });
                    dialogBuilder.setOKTextListener("去授权", (dialog, which) -> {
                        dialog.dismiss();
                        executor.execute();
                    });
                    activity.showDialog(dialogBuilder);
                }).onGranted(data -> callBack.onHasPermission()).onDenied(data -> {
            if (AndPermission.hasAlwaysDeniedPermission(activity,permissions)){
                callBack.onHasPermission();
                return;
            }

            if (AndPermission.hasAlwaysDeniedPermission(activity,permissions)){
                DialogBuilder dialogBuilder = new DialogBuilder();
                dialogBuilder.setMessage(toast);
                dialogBuilder.setCancelTextListener("取消", (dialog, which) -> {
                    dialog.dismiss();
                    callBack.onFailedPermission();
                });
                dialogBuilder.setOKTextListener("去授权", (dialog, which) -> {
                    dialog.dismiss();
                    AndPermission.with(activity).runtime().setting().start(requestCode);
                });
                activity.showDialog(dialogBuilder);
            }else{
                callBack.onFailedPermission();
            }
        }).start();
    }

    /**
     * 判断GPS是否开启，GPS开启的
     *
     * @param context
     * @return true 表示开启
     */
    public static final boolean isOpenGPS(final Context context) {

        boolean gps = false;
        try {
            android.location.LocationManager locationManager = (android.location.LocationManager) context.getSystemService (Context.LOCATION_SERVICE);
            // 通过GPS卫星定位，定位级别可以精确到街（通过24颗卫星定位，在室外和空旷的地方定位准确、速度快）
            gps = locationManager.isProviderEnabled (android.location.LocationManager.GPS_PROVIDER);
        } catch (Exception e) {
            gps = false;
        }

        return gps;
    }


    /***
     * 打开系统GPS开关设置
     * @param context
     */
    public static final void openGPS(Activity context) {

        try {
            // 打开GPS
            // 转到手机设置界面，用户设置GPS
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            // 设置完成后返回到原来的界面
            context.startActivityForResult (intent, 10000);
        } catch (Exception e) {
        }
    }
    /***
     * 打开应用GPS 权限设置
     * @param context
     */

    public static final void startAppSettings(Activity context) {

        try {
            // 启动应用的设置
            Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
            intent.setData (Uri.parse ("package:" + context.getApplication ().getPackageName ()));
            context.startActivity (intent);
        } catch (Exception e) {
        }
    }
}
